package com.itshen.one.consumer;

import com.itshen.one.producer.Producer;
import com.rabbitmq.client.Channel;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.core.ExchangeTypes;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.Exchange;
import org.springframework.amqp.rabbit.annotation.Queue;
import org.springframework.amqp.rabbit.annotation.QueueBinding;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Component;

import java.io.IOException;

/**
 * @author: stephen.shen
 * @create: 2018-12-13 9:31
 */
@Slf4j
@Component
public class Consumer {

    @RabbitListener(bindings = @QueueBinding(
            value = @Queue(value = "test.queue",durable = "true"), // queue持久化
            exchange = @Exchange(
                    value = "test.exchange",
                    ignoreDeclarationExceptions = "true",
                    durable = "true", // exchange持久化
                    type = ExchangeTypes.TOPIC),
            key = Producer.TEST_KEY
    ))
    public void receiveMessage(String msg, Message message, Channel channel){
        log.info("MQ receive message: {}",msg);
        try {
            //消息处理正常，没有抛出异常，手动应答消息
            channel.basicAck(message.getMessageProperties().getDeliveryTag(), false);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
